/**
 * 
 */
package com.nari.resconfig.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;

import com.nari.basic.model.IUser;
import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.resconfig.model.IProjectResourceConfig;

/**
 * 
 * @author Xiaopeng
 * @date 2014-5-9
 */
public class ProjectResourceConfigDaoImpl extends GenericDaoImpl<IProjectResourceConfig, String>
		implements IProjectResourceConfigDao {
	
	/**
	 * 根据用户信息删除对应的用户项目资源列表
	 * @param userUID
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-13
	 */
	public void deleteProjResAuthorityByUserInfo(String userUID) throws Exception {
		String hsql = "delete from IProjectResourceConfig prc " +
				"where prc.userUID='"+userUID+"'";
		this.runFormatHSql(hsql);
	}
	
	/**
	 * 根据项目信息删除对应的用户项目资源列表
	 * @param projectUID
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-13
	 */
	public void deleteProjResAuthorityByProjInfo(String projectUID) throws Exception {
		String hsql = "delete from IProjectResourceConfig prc " +
				"where prc.projectUID='"+projectUID+"'";
		this.runFormatHSql(hsql);
	}
	
	/**
	 * 查询项目资源已配置角色用户
	 * @param projectUID
	 * @param roleID
	 * @param userID
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-19
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<IUser> queryProjResRoleUser(String projectUID, String roleID, 
			String userID, PageCond pageCond) throws Exception {
		//分页查询结果
		int total = 0;
		List<IUser> userList = new ArrayList<IUser>();
		if((projectUID!=null && !"".equals(projectUID)) && (roleID!=null && !"".equals(roleID))) {
			//权限人员UID查询
			String authorityUserUIDHql = "select prc.userUID from IProjectResourceConfig prc " +
					"where prc.projectUID='"+projectUID+"' " +
					"and prc.roleUID=(select r.id from IRole r " +
									"where r.roleID='"+roleID+"')";
			//权限人员查询
			String listHql = "from IUser u where u.id in("+authorityUserUIDHql+")";
			//用户名
			if(userID!=null && !"".equals(userID)) {
				listHql += " and lower(u.userID) like '%"+userID.trim().toLowerCase()+"%'";
			}
			//结果集个数查询
			String countHql = "select count(*) " + listHql;
			total = ((Long)this.getCurrentSession().createQuery(countHql).uniqueResult()).intValue();
			//结果集排序
			listHql += " order by u.orderID asc,u.userID asc";
			//结果集分页
			Query listQuery = this.getCurrentSession().createQuery(listHql);
			listQuery.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
			//结果集查询
			userList = listQuery.list();
		}
		// 构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<IUser> userPage = new ResultWithPageCond<IUser>();
		userPage.setList(userList);
		userPage.setPageCond(pageCond);
		return userPage;
	}
	
	/**
	 * 查询可以配置项目资源的角色用户
	 * @param projectUID
	 * @param roleID
	 * @param userID
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-19
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<IUser> queryProbablyProjResRoleUser(String projectUID, 
			String roleID, String userID, PageCond pageCond) throws Exception {
		//分页查询结果
		int total = 0;
		List<IUser> userList = new ArrayList<IUser>();
		if((projectUID!=null && !"".equals(projectUID)) && (roleID!=null && !"".equals(roleID))) {
			//权限人员UID查询
			String authorityUserUIDHql = "select prc.userUID from IProjectResourceConfig prc " +
					"where prc.projectUID='"+projectUID+"' " +
					"and prc.roleUID=(select r.id from IRole r " +
									"where r.roleID='"+roleID+"')";
			//同等角色用户UID查询
			String roleUserUIDHql = "select ur.userUuid from IUserRole ur " +
					"where ur.roleUuid=(select r.id from IRole r " +
									"where r.roleID='"+roleID+"')";
			//权限人员查询
			String listHql = "from IUser u where u.id not in("+authorityUserUIDHql+") " +
					"and u.id in("+roleUserUIDHql+")";
			//用户名
			if(userID!=null && !"".equals(userID)) {
				listHql += " and lower(u.userID) like '%"+userID.trim().toLowerCase()+"%'";
			}
			//结果集个数查询
			String countHql = "select count(*) " + listHql;
			total = ((Long)this.getCurrentSession().createQuery(countHql).uniqueResult()).intValue();
			//结果集排序
			listHql += " order by u.orderID asc,u.userID asc";
			//结果集分页
			Query listQuery = this.getCurrentSession().createQuery(listHql);
			listQuery.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
			//结果集查询
			userList = listQuery.list();
		}
		// 构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<IUser> userPage = new ResultWithPageCond<IUser>();
		userPage.setList(userList);
		userPage.setPageCond(pageCond);
		return userPage;
	}
	
	/**
	 * 删除用户角色的资源授权
	 * @param projectUID
	 * @param roleUID
	 * @param userUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-19
	 */
	public ResponseMsg deleteAuthorityUser(String projectUID, String roleUID, 
			String userUID) throws Exception {
		String hsql = "delete from IProjectResourceConfig prc " +
				"where prc.projectUID='"+projectUID+"' " +
				"and prc.roleUID='"+roleUID+"' " +
				"and prc.userUID='"+userUID+"'";
		this.runFormatHSql(hsql);
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 批量设置用户角色的资源授权
	 * @param projectUID
	 * @param roleUID
	 * @param userUIDList
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-19
	 */
	public ResponseMsg saveAuthorityUserBatch(String projectUID, String roleUID, 
			String userUIDList) throws Exception {
		ResponseMsg rmsg = ResponseMsg.getInstance(true, "");
		if(userUIDList != null) {
			String[] userUIDArray = userUIDList.split(",");
			for(String userUID: userUIDArray) {
				IProjectResourceConfig config = new IProjectResourceConfig();
				config.setProjectUID(projectUID);
				config.setRoleUID(roleUID);
				config.setUserUID(userUID);
				config.setUpdateTime(new Date());
				this.saveEntity(config);
			}
		} else {
			rmsg = ResponseMsg.getInstance(false, "用户列表为空，无法进行项目资源配置！");
		}
		return rmsg;
	}
	
	/**
	 * 通过角色信息查询其所属的项目资源
	 * @param projectUID
	 * @param roleUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-19
	 */
	public List<IProjectResourceConfig> queryProjResByRoleInfo(String projectUID, 
			String roleUID) throws Exception {
		String hsql = "from IProjectResourceConfig prc " +
				"where prc.projectUID='"+projectUID+"' " +
				"and prc.roleUID='"+roleUID+"' " +
				"order by prc.updateTime desc";
		return this.runQueryHSql(hsql);
	}
}
